package com.example.hello;

import com.example.hello.client.Dropdown.BootstrapDropdownRpc;
import com.example.hello.client.Dropdown.BootstrapDropdownState;
import com.vaadin.shared.Connector;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.ui.AbstractComponentContainer;
import com.vaadin.ui.Component;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class BootstrapDropdown extends AbstractComponentContainer {

    private BootstrapDropdownRpc rpc = new BootstrapDropdownRpc() {
        public void click(MouseEventDetails details) {
            fireEvent(new BootstrapEvent(BootstrapDropdown.this));
        }
    };

    public BootstrapDropdown(String text) {
        setText(text);

        registerRpc(rpc);
    }

    public BootstrapDropdown(List<Component> navs) {
        setNavs(navs);

        registerRpc(rpc);
    }

    public BootstrapDropdown(Component... navs) {
        for (Component c : navs) addNav(c);

        registerRpc(rpc);
    }

    public void BootstrapDropdown(Component nav) {
        addNav(nav);
    }

    @Override
    protected BootstrapDropdownState getState() {
        return (BootstrapDropdownState) super.getState();
    }

    public List<Component> getNavs() {
        List<Component> navs = new LinkedList<Component>();
        Iterator<Connector> itr = getState().getNavs().iterator();
        while (itr.hasNext()) {
            navs.add((Component) itr.next());
        }

        return navs;
    }

    public void setNavs(List<Component> navs) {
        List<Connector> connectorList = new LinkedList<Connector>();
        Iterator<Component> itr = navs.iterator();
        while (itr.hasNext()) {
            connectorList.add(itr.next());
        }

        getState().setNavs(connectorList);
    }

    public void addNav(Component nav) {
        addNav(nav, getNavs().size());
    }

    public void addNav(Component nav, int index) {
        if (nav.getParent() != null) {
            throw new IllegalArgumentException("Given nav already has a parent component");
        }

        getState().addNav(nav, index);
        super.addComponent(nav);

        markAsDirty();
    }

    public void removeNav(Component nav) {
        if (!getNavs().contains(nav)) {
            return;
        }

        getState().removeNav(nav);
        super.removeComponent(nav);

        markAsDirty();
    }

    public String getText() {
        return getState().getText();
    }

    public void setText(String text) {
        getState().setText(text);
    }

    public String getBaseIcon() {
        return getState().getBaseIcon();
    }

    public void setBaseIcon(String baseIcon) {
        getState().setBaseIcon(baseIcon);
    }

    public String getDropdownIcon() {
        return getState().getIcon();
    }

    public void setDropdownIcon(String icon) {
        getState().setIcon(icon);
    }

    public BootstrapNavWidget.IconPosition getIconPosition() {
        return BootstrapNavWidget.IconPosition.valueOf(getState().getIconPosition());
    }

    public void setIconPosition(BootstrapNavWidget.IconPosition iconPosition) {
        getState().setIconPosition(iconPosition.toString());
    }

    public BootstrapNavWidget.IconSize getIconSize() {
        return BootstrapNavWidget.IconSize.valueOf(getState().getIconSize());
    }

    public void setIconSize(BootstrapNavWidget.IconSize size) {
        getState().setIconSize(size.toString());
    }

    public boolean isDropup() {
        return getState().isDropup();
    }

    public void setDropup(boolean dropup) {
        getState().setDropup(dropup);
    }

    public boolean isRightDropdown() {
        return getState().isRightDropdown();
    }

    public void setRightDropdown(boolean rightDropdown) {
        getState().setRightDropdown(rightDropdown);
    }

    @Override
    public void replaceComponent(Component component, Component component2) {

    }

    @Override
    public int getComponentCount() {
        return getNavs().size();
    }

    @Override
    public Iterator<Component> iterator() {
        return getNavs().iterator();
    }

}